package leetcode_100;

import java.util.Stack;

/**
 *@author 周杨
 *ValidParentheses_20 括号匹配 用栈
 *describe:AC 20%
 *2018年12月22日 下午3:51:24
 */
public class ValidParentheses_20 {
	public boolean isValid(String s) {
		Stack<Character> stack=new Stack<Character>();
		for(int i=0;i<s.length();++i) {
			char c=s.charAt(i);
			if(c=='('||c=='{'||c=='[')
				stack.push(c);
			else if(c==')') {
				if(stack.isEmpty()||stack.peek()!='(')
					return false;
				stack.pop();
			}
			else if(c=='}') {
				if(stack.isEmpty()||stack.peek()!='{')
					return false;
				stack.pop();
			}
			else { 
				if(stack.isEmpty()||stack.peek()!='[')
					return false;
				stack.pop();
			}
		}
		return stack.isEmpty();
	}
}
